<?php

/**
 * 国内商标档案模型
 */
class Trademark_model extends CI_Model {
    
    const TN = "trademarks";
    
    /**
     * 添加时生成的档案号
     * @var type 
     */
    private $_file_no;

    /**
     * 
     * @param type $id
     * @return array
     */
    public function get($id) {
        $query = $this->db->get_where(self::TN, array('tm_id' => $id));
        
        $tm = $query->first_row("array");
        $mix = unserialize($tm['mixture']);
        unset($tm['mixture']);
        
        return $tm + $mix;
    }

    /**
     * 新trademark数组
     * @return type
     */
    public function new_trademark() {
        return array(
            'file_no' => '保存时自动生成',
            'file_create_date' => date('Y-m-d'),
            'case_type' => CT_ZCSQ,
            'c_name' => '',
            'c_name_en' => '',
            'c_addr' => '',
            'c_addr_en' => '',
            'c_licence_number' => '',
            'c_country' => '',
            'other_client_info' => '',
            'agency' => '',
            'agent' => '',
            'partner' => '',
            'ct_contact' => '',
            'ct_telephone' => '',
            'ct_fax' => '',
            'ct_email' => '',
            'ct_qq' => '',
            'ct_mailing_addr' => '',
            'government_fee' => '',
            'agency_fee' => '',
            'total_fee' => '',
            'is_collected' => '否',
            'collection_date' => '',
            'collection_account' => '',
            'is_billed' => '否',
            'billing_date' => '',
            'billing_amount' => '',
            'drawer' => '',
            'confirmor' => '',
            'checkoff_date' => '',
        );
    }
    
    public function insert($trademark) {
        $trademark['file_no'] = $this->_get_new_file_no();
        $trademark['total_fee'] = floatval($trademark['government_fee']) + floatval($trademark['agency_fee']);
        $this->db->insert(self::TN, $trademark);
        $this->_file_no = $trademark['file_no'];
    }
    
    public function update($id, $trademark) {
        unset($trademark['file_no']);
        $trademark['total_fee'] = floatval($trademark['government_fee']) + floatval($trademark['agency_fee']);
        $this->db->where('tm_id', $id);
        $this->db->update(self::TN, $trademark);
    }
    
    public function delete($id) {
        $this->db->delete(self::TN, array('tm_id' => $id));
    }
    
    public function file_no() {
        return $this->_file_no;
    }
    
    public function insert_id() {
        return $this->db->insert_id();
    }

        /**
     * 案件类型
     * @return type
     */
    public function case_types() {
        return array(
            CT_ZCSQ, 
            CT_ZRSQ, 
            CT_BGSQ, 
            CT_XZSQ, 
            CT_BFZCZ, 
            CT_CHSQ, 
            CT_GZSQ, 
            CT_TGYXQ, 
            CT_TGZCZM, 
            CT_XKBA, 
            CT_BHFS, 
            CT_CSSQ, 
            CT_CSFS, 
            CT_CSFSDB, 
            CT_CSDB, 
            CT_YYDB, 
            CT_YYFS, 
            CT_YYFSDB, 
            CT_YYSQ, 
            CT_ZYSQ, 
            CT_ZYDB,
            CT_QTSQ,
            CT_QTAJ
        );
    }
    
    public function agencies() {
        return array('北京捷立信知识产权代理有限公司', '临汾市尧都区晟捷通商标代理事务所有限公司');
    }

    /**
     * 
     * @param type $conditions
     * @param type $per_page 默认0表示取所有记录
     * @param type $page
     * @return array
     */
    public function fetch_array($conditions = array(), $per_page = 0, $page = 0) {
        $this->_set_where($conditions);
        if ($per_page > 0) {
            $offset = ($page - 1) * $per_page;
            $this->db->limit($per_page, $offset);
        }
        $this->db->order_by('tm_id', 'desc');

        return $this->db->get(self::TN)->result_array();
    }
    
    /**
     * 返回记录总数
     * param $conditions
     */
    public function fetch_count($conditions) {
        $this->_set_where($conditions);
        $this->db->from(self::TN);
        
        return $this->db->count_all_results();
    }
    
    /**
     * 返回query
     * @param type $conditions
     * @return type
     */
    public function export_query($conditions) {
        $this->db->select('file_no as 档案号, file_create_date as 建档日期, c_name as 申请人, 
            tm_name as 商标, tm_class as 类别, application_no as 申请号, case_type as 案件类型, 
            agent as 代理人, partner as 合作所, government_fee as 官费, agency_fee as 代理费, 
            total_fee as 总费用, is_collected as 是否收款');
        $this->_set_where($conditions);
        $this->db->order_by('tm_id', 'desc');
        return $this->db->get(self::TN);
    }
    
    /**
     * 设置where
     * @param type $conditions
     */
    private function _set_where($conditions) {
        if (isset($conditions['c_name']) && $conditions['c_name'] != '') {
            $this->db->like('c_name', $conditions['c_name']);
        }
        if (isset($conditions['application_no']) && $conditions['application_no'] != '') {
            $this->db->like('application_no', $conditions['application_no']);
        }
        if (isset($conditions['tm_name']) && $conditions['tm_name'] != '') {
            $this->db->like('tm_name', $conditions['tm_name']);
        }
        
        if (!empty($conditions['case_type'])) {
            $this->db->where_in('case_type', $conditions['case_type']);
        }
        if (!empty($conditions['agent'])) {
            $this->db->where_in('agent', $conditions['agent']);
        }
        if (!empty($conditions['partner'])) {
            $this->db->where_in('partner', $conditions['partner']);
        }

        if (!empty($conditions['file_create_date_from'])) {
            $this->db->where('file_create_date >=', $conditions['file_create_date_from']);
        }
        if (!empty($conditions['file_create_date_to'])) {
            $this->db->where('file_create_date <=', $conditions['file_create_date_to']);
        }
    }
    
    /**
     * 取下一个档案号
     * 规则，DD + 六位数字，从000001开始，自动增加
     */
    private function _get_new_file_no() {
        $this->db->select_max('file_no');
        $query = $this->db->get(self::TN);
        $row = $query->first_row('array');
        if (empty($row)) {
            return 'DD000001';
        } else {
            return 'DD' . str_pad(intval(substr($row['file_no'], 2)) + 1, 6, '0', STR_PAD_LEFT);
        }
    }
}
